"use strict";
var initDb = require("./untils/initDb");
var Crawler = require("crawler");
var article = require('./model/job');
/*initDb();
var seq=require("./db");
seq
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.');
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err);
  });
*/
for (let index = 1; index <= 5; index +=1) {
 
    var URL = 'https://search.51job.com/list/130900%252C010000,000000,7501,01,9,99,%25E6%2595%25B0%25E6%258D%25AE%25E5%2588%2586%25E6%259E%2590%25E5%25B8%2588,2,'+ index+'.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare=';
    var c = new Crawler({
      rateLimit:2000,
      maxConnections : 1,
      //maxConnections: 10,
      // This will be called for each crawled page
      callback: function (error, res, done) {
        if (error) {
          console.log(error);
        } else {
          var $ = res.$;
          var list = $('.dw_table .el').toArray();
          list.forEach(element => {
            var jobName = $(element).find('.t1 a').attr('title');
            var companyName = $(element).find('.t2 a').text().trim().replace(/[ \n\r]/g, '');
            var workplace =$(element).find('.t3').text().trim().replace(/[ \n\r]/g, '');;
            var pay = $(element).find('.t4').text().trim().replace(/[ \n\r]/g, '');;
            var Time = $(element).find('.t5').text().trim().replace(/[ \n\r]/g, '');;
            console.log(jobName,companyName,workplace,pay,Time);
            article.create({jobName:jobName,companyName:companyName,workplace:workplace,pay:pay,Time:Time})
          });
        }
        done();
      }
    });
    c.queue(URL);
  }